package com.msf.tutorial.java8.chapter8;

import java.util.Arrays;
import java.util.List;
import java.util.stream.IntStream;
import java.util.stream.Stream;

/**
 * 数值流
 *
 * IntStream,LongStream,DoubleStream
 *
 * 主要用于处理装箱的效率差异
 */
public class C01Use {

    /**
     * mapToInt
     * 对象流转换为数值流
     */
    public static void mapToInt() {
        List<Integer> list = Arrays.asList(1, 2, 3, 4, 5);
        list.stream().mapToInt(i -> i);
    }

    /**
     * boxed
     * 数值流转换为对象流
     */
    public static void boxed() {
        List<Integer> list = Arrays.asList(1, 2, 3, 4, 5);
        IntStream is = list.stream().mapToInt(i -> i);

        Stream<Integer> si = is.boxed();
    }

    /**
     * 使用IntStream求和
     */
    public static int sum() {
        List<Integer> list = Arrays.asList(1, 2, 3, 4, 5);
        return list.stream().mapToInt(i -> i).sum();
    }

    /**
     * 使用IntStream最大值
     */
    public static int max() {
        List<Integer> list = Arrays.asList(1, 2, 3, 4, 5);
        return list.stream().mapToInt(i -> i).max().orElse(0);
    }

    /**
     * 使用IntStream最小值
     */
    public static int min() {
        List<Integer> list = Arrays.asList(1, 2, 3, 4, 5);
        return list.stream().mapToInt(i -> i).min().orElse(0);
    }

    /**
     * 数值范围 [1,20)
     */
    public static void range() {
        IntStream evenNumbers = IntStream.range(1, 20).filter(n -> n % 2 == 0);
        evenNumbers.forEach(System.out::println);
    }

    /**
     * 数值范围 [1,20]
     */
    public static void rangeClosed() {
        IntStream evenNumbers = IntStream.rangeClosed(1, 20).filter(n -> n % 2 == 0);
        evenNumbers.forEach(System.out::println);
    }

    public static void main(String[] args) {
        System.out.println(1.3%1);
    }

}
